package com.hx_ai.nlp.simple.utils;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static sun.font.FontUtilities.isWindows;

/**
 * Created by dzkan on 2016/11/18.
 * 载入股票的固定属性
 */
@Getter
@Slf4j
public class StockProperties {
    private Map<String, String> nameToId;
    private List<String> negative;
    private Map<String, Long> dateToDay;

    private Map<String, String> stockProps;

    private void setupNameAndId() {
        nameToId = new HashMap<>();
        List<String> lines = new ArrayList<>();

        Charset charset = StandardCharsets.UTF_8;
        if (isWindows){
            charset = StandardCharsetsNew.GBK;
        }

        try {
            InputStreamLoadUtils.inputSteamLoad("/sh_3.txt", charset, lines);
            InputStreamLoadUtils.inputSteamLoad("/sz_3.txt", charset, lines);
            if (!lines.isEmpty()){
                for (String line : lines) {
                    String[] stock = line.split(",");
                    nameToId.putIfAbsent(stock[0], stock[1]);
                }
            }
        } catch (Exception e) {
            log.info("===>setupNameAndId-解析txt文件异常, errMsg={}<===", e.getMessage());
            e.printStackTrace();
        }
    }

    private void setupNegative() {
        String[] negativeWords = {"不想", "不喜欢", "不要", "不爱", "别", "不能"};
        negative = Arrays.asList(negativeWords);
    }

    private void setupDate() {
        dateToDay = new HashMap<>();

        Date today = new Date();
        String[] dates = {"前天", "昨天", "今天", "明天", "后天", "前日", "昨日", "今日", "明日", "后日"};
        int[] dateId = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
        for (int i = 0; i < dates.length; i++) {
            dateToDay.put(dates[i], today.getTime() + dateId[i] * 86400000);
        }

        String[] weekCh1 = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        String[] weekCh2 = {"礼拜天", "礼拜一", "礼拜二", "礼拜三", "礼拜四", "礼拜五", "礼拜六"};
        String[] weekCh3 = {"星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        int dayOfWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
        for (int i = 0; i < weekCh1.length; i++) {
            Date dt = new Date();
            int dd = i <= dayOfWeek ? i - dayOfWeek : i - dayOfWeek - 7;
            dt.setTime(today.getTime() + dd * 86400000);
            dateToDay.put(weekCh1[i], dt.getTime());
            dateToDay.put(weekCh2[i], dt.getTime());
            dateToDay.put(weekCh3[i], dt.getTime());
        }
    }

    private void setupProps() {
        stockProps = new HashMap<>();
        String[] props = {"开盘", "收盘", "买点", "卖点"};
        String[] propKeys = {"opprice", "clprice", "bpprice", "cpprice"};
        for (int i = 0; i < props.length; i++) {
            stockProps.put(propKeys[i], props[i]);
        }
    }

    public StockProperties() {
        setupNameAndId();
        setupNegative();
        setupDate();
        setupProps();
    }

}
